[bug-62018] use ints to index fonts

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1824826 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-02-20 00:25:43 +00:00
parent 1f0cf7d6e2
commit 104d4dac06
20 changed files with 337 additions and 237 deletions

View File

@ -40,173 +40,176 @@ import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/** /**
* Sheet Viewer Table Cell Editor -- not commented via javadoc as it * Sheet Viewer Table Cell Editor -- not commented via javadoc as it
* nearly completely consists of overridden methods. * nearly completely consists of overridden methods.
* *
* @author Jason Height * @author Jason Height
*/ */
public class SVTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { public class SVTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
private static final Color black = getAWTColor(HSSFColorPredefined.BLACK); private static final Color black = getAWTColor(HSSFColorPredefined.BLACK);
private static final Color white = getAWTColor(HSSFColorPredefined.WHITE); private static final Color white = getAWTColor(HSSFColorPredefined.WHITE);
private static final POILogger logger = POILogFactory.getLogger(SVTableCellEditor.class);
private HSSFWorkbook wb; private HSSFWorkbook wb;
private JTextField editor; private JTextField editor;
public SVTableCellEditor(HSSFWorkbook wb) { public SVTableCellEditor(HSSFWorkbook wb) {
this.wb = wb; this.wb = wb;
this.editor = new JTextField(); this.editor = new JTextField();
}
/**
* Gets the cellEditable attribute of the SVTableCellEditor object
*
* @return The cellEditable value
*/
@Override
public boolean isCellEditable(java.util.EventObject e) {
if (e instanceof MouseEvent) {
return ((MouseEvent) e).getClickCount() >= 2;
} }
return false;
}
@Override /**
public boolean shouldSelectCell(EventObject anEvent) { * Gets the cellEditable attribute of the SVTableCellEditor object
return true; *
} * @return The cellEditable value
*/
@Override
public boolean startCellEditing(EventObject anEvent) { public boolean isCellEditable(java.util.EventObject e) {
System.out.println("Start Cell Editing"); if (e instanceof MouseEvent) {
return true; return ((MouseEvent) e).getClickCount() >= 2;
} }
return false;
}
@Override
public boolean stopCellEditing() {
System.out.println("Stop Cell Editing"); @Override
fireEditingStopped(); public boolean shouldSelectCell(EventObject anEvent) {
return true; return true;
} }
@Override public boolean startCellEditing(EventObject anEvent) {
public void cancelCellEditing() { logger.log(POILogger.INFO, "Start Cell Editing");
System.out.println("Cancel Cell Editing"); return true;
fireEditingCanceled(); }
}
@Override
@Override public boolean stopCellEditing() {
public void actionPerformed(ActionEvent e) { logger.log(POILogger.INFO, "Stop Cell Editing");
System.out.println("Action performed"); fireEditingStopped();
stopCellEditing(); return true;
} }
/** @Override
* Gets the cellEditorValue attribute of the SVTableCellEditor object public void cancelCellEditing() {
* logger.log(POILogger.INFO, "Cancel Cell Editing");
* @return The cellEditorValue value fireEditingCanceled();
*/ }
@Override
public Object getCellEditorValue() {
System.out.println("GetCellEditorValue"); @Override
//JMH Look at when this method is called. Should it return a HSSFCell? public void actionPerformed(ActionEvent e) {
return editor.getText(); logger.log(POILogger.INFO, "Action performed");
} stopCellEditing();
}
/**
* Gets the tableCellEditorComponent attribute of the SVTableCellEditor object /**
* * Gets the cellEditorValue attribute of the SVTableCellEditor object
* @return The tableCellEditorComponent value *
*/ * @return The cellEditorValue value
@Override */
public Component getTableCellEditorComponent(JTable table, Object value, @Override
boolean isSelected, public Object getCellEditorValue() {
int row, logger.log(POILogger.INFO, "GetCellEditorValue");
int column) { //JMH Look at when this method is called. Should it return a HSSFCell?
System.out.println("GetTableCellEditorComponent"); return editor.getText();
HSSFCell cell = (HSSFCell) value; }
if (cell != null) {
HSSFCellStyle style = cell.getCellStyle();
HSSFFont f = wb.getFontAt(style.getFontIndex()); /**
boolean isbold = f.getBold(); * Gets the tableCellEditorComponent attribute of the SVTableCellEditor object
boolean isitalics = f.getItalic(); *
* @return The tableCellEditorComponent value
int fontstyle = Font.PLAIN; */
@Override
if (isbold) { public Component getTableCellEditorComponent(JTable table, Object value,
fontstyle = Font.BOLD; boolean isSelected,
} int row,
if (isitalics) { int column) {
fontstyle = fontstyle | Font.ITALIC; logger.log(POILogger.INFO, "GetTableCellEditorComponent");
} HSSFCell cell = (HSSFCell) value;
if (cell != null) {
int fontheight = f.getFontHeightInPoints(); HSSFCellStyle style = cell.getCellStyle();
if (fontheight == 9) { HSSFFont f = wb.getFontAt(style.getFontIntIndex());
fontheight = 10; //fix for stupid ol Windows boolean isbold = f.getBold();
} boolean isitalics = f.getItalic();
Font font = new Font(f.getFontName(),fontstyle,fontheight); int fontstyle = Font.PLAIN;
editor.setFont(font);
if (isbold) {
if (style.getFillPattern() == FillPatternType.SOLID_FOREGROUND) { fontstyle = Font.BOLD;
editor.setBackground(getAWTColor(style.getFillForegroundColor(), white)); }
} else { if (isitalics) {
editor.setBackground(white); fontstyle = fontstyle | Font.ITALIC;
} }
editor.setForeground(getAWTColor(f.getColor(), black)); int fontheight = f.getFontHeightInPoints();
if (fontheight == 9) {
fontheight = 10; //fix for stupid ol Windows
//Set the value that is rendered for the cell }
switch (cell.getCellType()) {
case BLANK: Font font = new Font(f.getFontName(), fontstyle, fontheight);
editor.setText(""); editor.setFont(font);
break;
case BOOLEAN: if (style.getFillPattern() == FillPatternType.SOLID_FOREGROUND) {
if (cell.getBooleanCellValue()) { editor.setBackground(getAWTColor(style.getFillForegroundColor(), white));
editor.setText("true"); } else {
} else { editor.setBackground(white);
editor.setText("false"); }
}
break; editor.setForeground(getAWTColor(f.getColor(), black));
case NUMERIC:
editor.setText(Double.toString(cell.getNumericCellValue()));
break; //Set the value that is rendered for the cell
case STRING: switch (cell.getCellType()) {
editor.setText(cell.getRichStringCellValue().getString()); case BLANK:
break; editor.setText("");
case FORMULA: break;
default: case BOOLEAN:
editor.setText("?"); if (cell.getBooleanCellValue()) {
} editor.setText("true");
switch (style.getAlignment()) { } else {
case LEFT: editor.setText("false");
case JUSTIFY: }
case FILL: break;
editor.setHorizontalAlignment(SwingConstants.LEFT); case NUMERIC:
break; editor.setText(Double.toString(cell.getNumericCellValue()));
case CENTER: break;
case CENTER_SELECTION: case STRING:
editor.setHorizontalAlignment(SwingConstants.CENTER); editor.setText(cell.getRichStringCellValue().getString());
break; break;
case GENERAL: case FORMULA:
case RIGHT: default:
editor.setHorizontalAlignment(SwingConstants.RIGHT); editor.setText("?");
break; }
default: switch (style.getAlignment()) {
editor.setHorizontalAlignment(SwingConstants.LEFT); case LEFT:
break; case JUSTIFY:
} case FILL:
editor.setHorizontalAlignment(SwingConstants.LEFT);
break;
case CENTER:
case CENTER_SELECTION:
editor.setHorizontalAlignment(SwingConstants.CENTER);
break;
case GENERAL:
case RIGHT:
editor.setHorizontalAlignment(SwingConstants.RIGHT);
break;
default:
editor.setHorizontalAlignment(SwingConstants.LEFT);
break;
}
}
return editor;
} }
return editor;
}
} }

View File

@ -140,7 +140,7 @@ public class SVTableCellRenderer extends JLabel
@Override @Override
public Component getTableCellRendererComponent(JTable table, Object value, public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) { boolean isSelected, boolean hasFocus, int row, int column) {
boolean isBorderSet = false; boolean isBorderSet = false;
//If the JTables default cell renderer has been setup correctly the //If the JTables default cell renderer has been setup correctly the
//value will be the HSSFCell that we are trying to render //value will be the HSSFCell that we are trying to render
@ -148,7 +148,7 @@ public class SVTableCellRenderer extends JLabel
if (c != null) { if (c != null) {
HSSFCellStyle s = c.getCellStyle(); HSSFCellStyle s = c.getCellStyle();
HSSFFont f = wb.getFontAt(s.getFontIndex()); HSSFFont f = wb.getFontAt(s.getFontIntIndex());
setFont(SVTableUtils.makeFont(f)); setFont(SVTableUtils.makeFont(f));
if (s.getFillPattern() == FillPatternType.SOLID_FOREGROUND) { if (s.getFillPattern() == FillPatternType.SOLID_FOREGROUND) {

View File

@ -63,7 +63,7 @@ public class CellStyleDetails {
System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " "); System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " "); System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");
Font font = wb.getFontAt(style.getFontIndex()); Font font = wb.getFontAt(style.getFontIntIndex());
System.out.print("Font=" + font.getFontName() + " "); System.out.print("Font=" + font.getFontName() + " ");
System.out.print("FontColor="); System.out.print("FontColor=");
if (font instanceof HSSFFont) { if (font instanceof HSSFFont) {

View File

@ -300,7 +300,7 @@ public class ToHtml {
} }
private void fontStyle(CellStyle style) { private void fontStyle(CellStyle style) {
Font font = wb.getFontAt(style.getFontIndex()); Font font = wb.getFontAt(style.getFontIntIndex());
if (font.getBold()) { if (font.getBold()) {
out.format(" font-weight: bold;%n"); out.format(" font-weight: bold;%n");

View File

@ -1660,7 +1660,7 @@ public final class ExtendedFormatRecord
@Override @Override
public String toString() public String toString()
{ {
StringBuffer buffer = new StringBuffer(); StringBuilder buffer = new StringBuilder();
buffer.append("[EXTENDEDFORMAT]\n"); buffer.append("[EXTENDEDFORMAT]\n");
if (getXFType() == XF_STYLE) if (getXFType() == XF_STYLE)

View File

@ -169,7 +169,7 @@ public final class HSSFCellStyle implements CellStyle {
* set the font for this style * set the font for this style
* @param font a font object created or retrieved from the HSSFWorkbook object * @param font a font object created or retrieved from the HSSFWorkbook object
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont() * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont()
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int)
*/ */
@Override @Override
public void setFont(Font font) { public void setFont(Font font) {
@ -186,19 +186,31 @@ public final class HSSFCellStyle implements CellStyle {
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short)
*/ */
@Override @Override
@Deprecated
public short getFontIndex() public short getFontIndex()
{ {
return _format.getFontIndex(); return _format.getFontIndex();
} }
/**
* gets the index of the font for this style
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int)
* @since 4.0.0
*/
@Override
public int getFontIntIndex()
{
return _format.getFontIndex();
}
/** /**
* gets the font for this style * gets the font for this style
* @param parentWorkbook The HSSFWorkbook that this style belongs to * @param parentWorkbook The HSSFWorkbook that this style belongs to
* @see org.apache.poi.hssf.usermodel.HSSFCellStyle#getFontIndex() * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#getFontIntIndex()
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int)
*/ */
public HSSFFont getFont(org.apache.poi.ss.usermodel.Workbook parentWorkbook) { public HSSFFont getFont(org.apache.poi.ss.usermodel.Workbook parentWorkbook) {
return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIndex()); return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIntIndex());
} }
/** /**

View File

@ -25,7 +25,7 @@ import org.apache.poi.ss.usermodel.Font;
* Represents a Font used in a workbook. * Represents a Font used in a workbook.
* *
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont() * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont()
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int)
* @see org.apache.poi.hssf.usermodel.HSSFCellStyle#setFont(HSSFFont) * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#setFont(HSSFFont)
*/ */
public final class HSSFFont implements Font { public final class HSSFFont implements Font {
@ -46,12 +46,12 @@ public final class HSSFFont implements Font {
public final static String FONT_ARIAL = "Arial"; public final static String FONT_ARIAL = "Arial";
private FontRecord font; private FontRecord font;
private short index; private int index;
/** Creates a new instance of HSSFFont */ /** Creates a new instance of HSSFFont */
protected HSSFFont(short index, FontRecord rec) protected HSSFFont(int index, FontRecord rec)
{ {
font = rec; font = rec;
this.index = index; this.index = index;
@ -85,7 +85,15 @@ public final class HSSFFont implements Font {
* unless you're comparing which one is which) * unless you're comparing which one is which)
*/ */
public short getIndex() public short getIndex() { return (short)index; }
/**
* get the index within the HSSFWorkbook (sequence within the collection of Font objects)
* @return unique index number of the underlying record this Font represents (probably you don't care
* unless you're comparing which one is which)
*/
public int getIndexAsInt()
{ {
return index; return index;
} }

View File

@ -178,13 +178,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
* this holds the HSSFFont objects attached to this workbook. * this holds the HSSFFont objects attached to this workbook.
* We only create these from the low level records as required. * We only create these from the low level records as required.
*/ */
private Map<Short,HSSFFont> fonts; private Map<Integer,HSSFFont> fonts;
/** /**
* holds whether or not to preserve other nodes in the POIFS. Used * holds whether or not to preserve other nodes in the POIFS. Used
* for macros and embedded objects. * for macros and embedded objects.
*/ */
private boolean preserveNodes; private boolean preserveNodes;
/** /**
* Used to keep track of the data formatter so that all * Used to keep track of the data formatter so that all
@ -1171,13 +1171,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
public HSSFFont createFont() public HSSFFont createFont()
{ {
/*FontRecord font =*/ workbook.createNewFont(); /*FontRecord font =*/ workbook.createNewFont();
short fontindex = (short) (getNumberOfFonts() - 1); int fontindex = getNumberOfFontsAsInt() - 1;
if (fontindex > 3) if (fontindex > 3)
{ {
fontindex++; // THERE IS NO FOUR!! fontindex++; // THERE IS NO FOUR!!
} }
if(fontindex == Short.MAX_VALUE){ if(fontindex >= Short.MAX_VALUE){
throw new IllegalArgumentException("Maximum number of fonts was exceeded"); throw new IllegalArgumentException("Maximum number of fonts was exceeded");
} }
@ -1194,8 +1194,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
String name, boolean italic, boolean strikeout, String name, boolean italic, boolean strikeout,
short typeOffset, byte underline) short typeOffset, byte underline)
{ {
short numberOfFonts = getNumberOfFonts(); int numberOfFonts = getNumberOfFontsAsInt();
for (short i=0; i<=numberOfFonts; i++) { for (int i = 0; i <= numberOfFonts; i++) {
// Remember - there is no 4! // Remember - there is no 4!
if(i == 4) { if(i == 4) {
continue; continue;
@ -1218,24 +1218,25 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
return null; return null;
} }
/**
* get the number of fonts in the font table
* @return number of fonts
*/
@Override @Override
public short getNumberOfFonts() @Deprecated
{ public short getNumberOfFonts() {
return (short) workbook.getNumberOfFontRecords(); return (short)getNumberOfFontsAsInt();
} }
/**
* Get the font at the given index number
* @param idx index number
* @return HSSFFont at the index
*/
@Override @Override
public int getNumberOfFontsAsInt() {
return workbook.getNumberOfFontRecords();
}
@Override
@Deprecated
public HSSFFont getFontAt(short idx) { public HSSFFont getFontAt(short idx) {
return getFontAt((int)idx);
}
@Override
public HSSFFont getFontAt(int idx) {
if(fonts == null) { if(fonts == null) {
fonts = new HashMap<>(); fonts = new HashMap<>();
} }
@ -1243,7 +1244,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
// So we don't confuse users, give them back // So we don't confuse users, give them back
// the same object every time, but create // the same object every time, but create
// them lazily // them lazily
Short sIdx = Short.valueOf(idx); Integer sIdx = Integer.valueOf(idx);
if(fonts.containsKey(sIdx)) { if(fonts.containsKey(sIdx)) {
return fonts.get(sIdx); return fonts.get(sIdx);
} }

View File

@ -49,16 +49,25 @@ public interface CellStyle {
* set the font for this style * set the font for this style
* @param font a font object created or retrieved from the Workbook object * @param font a font object created or retrieved from the Workbook object
* @see Workbook#createFont() * @see Workbook#createFont()
* @see Workbook#getFontAt(short) * @see Workbook#getFontAt(int)
*/ */
void setFont(Font font); void setFont(Font font);
/** /**
* gets the index of the font for this style * gets the index of the font for this style
* @see Workbook#getFontAt(short) * @see Workbook#getFontAt(short)
* @deprecated use <code>getFontIntIndex()</code> instead
*/ */
@Removal(version = "4.2")
short getFontIndex(); short getFontIndex();
/**
* gets the index of the font for this style
* @see Workbook#getFontAt(int)
* @since 4.0.0
*/
int getFontIntIndex();
/** /**
* set the cell's using this style to be hidden * set the cell's using this style to be hidden
* @param hidden - whether the cell using this style should be hidden * @param hidden - whether the cell using this style should be hidden

View File

@ -18,6 +18,8 @@
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import org.apache.poi.util.Removal;
public interface Font { public interface Font {
/** /**
@ -267,9 +269,20 @@ public interface Font {
* *
* @return unique index number of the underlying record this Font represents (probably you don't care * @return unique index number of the underlying record this Font represents (probably you don't care
* unless you're comparing which one is which) * unless you're comparing which one is which)
* @deprecated use <code>getIndexAsInt()</code> instead
*/ */
@Removal(version = "4.2")
public short getIndex(); public short getIndex();
/**
* get the index within the XSSFWorkbook (sequence within the collection of Font objects)
*
* @return unique index number of the underlying record this Font represents (probably you don't care
* unless you're comparing which one is which)
* @since 4.0.0
*/
public int getIndexAsInt();
public void setBold(boolean bold); public void setBold(boolean bold);
public boolean getBold(); public boolean getBold();

View File

@ -265,17 +265,38 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
* Get the number of fonts in the font table * Get the number of fonts in the font table
* *
* @return number of fonts * @return number of fonts
* @deprecated use <code>getNumberOfFontsAsInt()</code> instead
*/ */
@Removal(version = "4.2")
short getNumberOfFonts(); short getNumberOfFonts();
/**
* Get the number of fonts in the font table
*
* @return number of fonts
* @since 4.0.0
*/
int getNumberOfFontsAsInt();
/** /**
* Get the font at the given index number * Get the font at the given index number
* *
* @param idx index number (0-based) * @param idx index number (0-based)
* @return font at the index * @return font at the index
* @deprecated use <code>getFontAt(int)</code>
*/ */
@Removal(version = "4.2")
Font getFontAt(short idx); Font getFontAt(short idx);
/**
* Get the font at the given index number
*
* @param idx index number (0-based)
* @return font at the index
* @since 4.0.0
*/
Font getFontAt(int idx);
/** /**
* Create a new Cell style and add it to the workbook's style table * Create a new Cell style and add it to the workbook's style table
* *

View File

@ -234,7 +234,7 @@ public final class CellUtil {
public static void setFont(Cell cell, Font font) { public static void setFont(Cell cell, Font font) {
// Check if font belongs to workbook // Check if font belongs to workbook
Workbook wb = cell.getSheet().getWorkbook(); Workbook wb = cell.getSheet().getWorkbook();
final short fontIndex = font.getIndex(); final int fontIndex = font.getIndexAsInt();
if (!wb.getFontAt(fontIndex).equals(font)) { if (!wb.getFontAt(fontIndex).equals(font)) {
throw new IllegalArgumentException("Font does not belong to this workbook"); throw new IllegalArgumentException("Font does not belong to this workbook");
} }
@ -408,7 +408,7 @@ public final class CellUtil {
style.setFillPattern(getFillPattern(properties, FILL_PATTERN)); style.setFillPattern(getFillPattern(properties, FILL_PATTERN));
style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR)); style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR));
style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR)); style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR));
style.setFont(workbook.getFontAt(getShort(properties, FONT))); style.setFont(workbook.getFontAt(getInt(properties, FONT)));
style.setHidden(getBoolean(properties, HIDDEN)); style.setHidden(getBoolean(properties, HIDDEN));
style.setIndention(getShort(properties, INDENTION)); style.setIndention(getShort(properties, INDENTION));
style.setLeftBorderColor(getShort(properties, LEFT_BORDER_COLOR)); style.setLeftBorderColor(getShort(properties, LEFT_BORDER_COLOR));
@ -429,8 +429,24 @@ public final class CellUtil {
*/ */
private static short getShort(Map<String, Object> properties, String name) { private static short getShort(Map<String, Object> properties, String name) {
Object value = properties.get(name); Object value = properties.get(name);
if (value instanceof Short) { if (value instanceof Number) {
return ((Short) value).shortValue(); return ((Number) value).shortValue();
}
return 0;
}
/**
* Utility method that returns the named int value form the given map.
*
* @param properties map of named properties (String -> Object)
* @param name property name
* @return zero if the property does not exist, or is not a {@link Integer}
* otherwise the property value
*/
private static int getInt(Map<String, Object> properties, String name) {
Object value = properties.get(name);
if (value instanceof Number) {
return ((Number) value).intValue();
} }
return 0; return 0;
} }

View File

@ -144,7 +144,7 @@ public class SheetUtil {
if (cellType == CellType.FORMULA) if (cellType == CellType.FORMULA)
cellType = cell.getCachedFormulaResultType(); cellType = cell.getCachedFormulaResultType();
Font font = wb.getFontAt(style.getFontIndex()); Font font = wb.getFontAt(style.getFontIntIndex());
double width = -1; double width = -1;
if (cellType == CellType.STRING) { if (cellType == CellType.STRING) {
@ -266,7 +266,7 @@ public class SheetUtil {
*/ */
@Internal @Internal
public static int getDefaultCharWidth(final Workbook wb) { public static int getDefaultCharWidth(final Workbook wb) {
Font defaultFont = wb.getFontAt((short) 0); Font defaultFont = wb.getFontAt( 0);
AttributedString str = new AttributedString(String.valueOf(defaultChar)); AttributedString str = new AttributedString(String.valueOf(defaultChar));
copyAttributes(defaultFont, str, 0, 1); copyAttributes(defaultFont, str, 0, 1);

View File

@ -811,27 +811,28 @@ public class SXSSFWorkbook implements Workbook {
{ {
return _wb.findFont(bold, color, fontHeight, name, italic, strikeout, typeOffset, underline); return _wb.findFont(bold, color, fontHeight, name, italic, strikeout, typeOffset, underline);
} }
/**
* Get the number of fonts in the font table
*
* @return number of fonts
*/
@Override @Override
public short getNumberOfFonts() @Deprecated
{ public short getNumberOfFonts() {
return _wb.getNumberOfFonts(); return (short)getNumberOfFontsAsInt();
} }
/**
* Get the font at the given index number
*
* @param idx index number (0-based)
* @return font at the index
*/
@Override @Override
public int getNumberOfFontsAsInt()
{
return _wb.getNumberOfFontsAsInt();
}
@Override
@Deprecated
public Font getFontAt(short idx) public Font getFontAt(short idx)
{
return getFontAt((int)idx);
}
@Override
public Font getFontAt(int idx)
{ {
return _wb.getFontAt(idx); return _wb.getFontAt(idx);
} }

View File

@ -469,10 +469,23 @@ public class XSSFCellStyle implements CellStyle {
* @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getFontAt(short) * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getFontAt(short)
*/ */
@Override @Override
@Deprecated
public short getFontIndex() { public short getFontIndex() {
return (short) getFontId(); return (short) getFontId();
} }
/**
* Gets the index of the font for this style
*
* @return short - font index
* @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getFontAt(int)
* @since 4.0.0
*/
@Override
public int getFontIntIndex() {
return getFontId();
}
/** /**
* Get whether the cell's using this style are to be hidden * Get whether the cell's using this style are to be hidden
* *

View File

@ -63,7 +63,7 @@ public class XSSFFont implements Font {
private IndexedColorMap _indexedColorMap; private IndexedColorMap _indexedColorMap;
private ThemesTable _themes; private ThemesTable _themes;
private CTFont _ctFont; private CTFont _ctFont;
private short _index; private int _index;
/** /**
* Create a new XSSFFont * Create a new XSSFFont
@ -615,12 +615,14 @@ public class XSSFFont implements Font {
setFamily(family.getValue()); setFamily(family.getValue());
} }
/** @Override
* get the index within the XSSFWorkbook (sequence within the collection of Font objects) @Deprecated
* @return unique index number of the underlying record this Font represents (probably you don't care public short getIndex() {
* unless you're comparing which one is which) return (short)getIndexAsInt();
*/ }
public short getIndex()
@Override
public int getIndexAsInt()
{ {
return _index; return _index;
} }

View File

@ -925,6 +925,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
/** /**
* Finds a font that matches the one with the supplied attributes * Finds a font that matches the one with the supplied attributes
*
* @return the font with the matched attributes or <code>null</code>
*/ */
@Override @Override
public XSSFFont findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { public XSSFFont findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
@ -972,17 +974,16 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
return stylesSource.getStyleAt(idx); return stylesSource.getStyleAt(idx);
} }
/**
* Get the font at the given index number
*
* @param idx index number
* @return XSSFFont at the index
*/
@Override @Override
public XSSFFont getFontAt(short idx) { public XSSFFont getFontAt(short idx) {
return stylesSource.getFontAt(idx); return stylesSource.getFontAt(idx);
} }
@Override
public XSSFFont getFontAt(int idx) {
return stylesSource.getFontAt(idx);
}
/** /**
* Get the first named range with the given name. * Get the first named range with the given name.
* *
@ -1075,13 +1076,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
return stylesSource.getNumCellStyles(); return stylesSource.getNumCellStyles();
} }
/**
* Get the number of fonts in the this workbook
*
* @return number of fonts
*/
@Override @Override
public short getNumberOfFonts() { public short getNumberOfFonts() {
return (short)getNumberOfFontsAsInt();
}
@Override
public int getNumberOfFontsAsInt() {
return (short)stylesSource.getFonts().size(); return (short)stylesSource.getFonts().size();
} }

View File

@ -1013,19 +1013,19 @@ public abstract class BaseTestBugzillaIssues {
Workbook wb = _testDataProvider.createWorkbook(); Workbook wb = _testDataProvider.createWorkbook();
int startingFonts = wb instanceof HSSFWorkbook ? 4 : 1; int startingFonts = wb instanceof HSSFWorkbook ? 4 : 1;
assertEquals(startingFonts, wb.getNumberOfFonts()); assertEquals(startingFonts, wb.getNumberOfFontsAsInt());
// Get a font, and slightly change it // Get a font, and slightly change it
Font a = wb.createFont(); Font a = wb.createFont();
assertEquals(startingFonts+1, wb.getNumberOfFonts()); assertEquals(startingFonts+1, wb.getNumberOfFontsAsInt());
a.setFontHeightInPoints((short)23); a.setFontHeightInPoints((short)23);
assertEquals(startingFonts+1, wb.getNumberOfFonts()); assertEquals(startingFonts+1, wb.getNumberOfFontsAsInt());
// Get two more, unchanged // Get two more, unchanged
/*Font b =*/ wb.createFont(); /*Font b =*/ wb.createFont();
assertEquals(startingFonts+2, wb.getNumberOfFonts()); assertEquals(startingFonts+2, wb.getNumberOfFontsAsInt());
/*Font c =*/ wb.createFont(); /*Font c =*/ wb.createFont();
assertEquals(startingFonts+3, wb.getNumberOfFonts()); assertEquals(startingFonts+3, wb.getNumberOfFontsAsInt());
wb.close(); wb.close();
} }

View File

@ -276,7 +276,7 @@ public abstract class BaseTestCell {
cs = c.getCellStyle(); cs = c.getCellStyle();
assertNotNull("Formula Cell Style", cs); assertNotNull("Formula Cell Style", cs);
assertEquals("Font Index Matches", f.getIndex(), cs.getFontIndex()); assertEquals("Font Index Matches", f.getIndexAsInt(), cs.getFontIndex());
assertEquals("Top Border", BorderStyle.THIN, cs.getBorderTop()); assertEquals("Top Border", BorderStyle.THIN, cs.getBorderTop());
assertEquals("Left Border", BorderStyle.THIN, cs.getBorderLeft()); assertEquals("Left Border", BorderStyle.THIN, cs.getBorderLeft());
assertEquals("Right Border", BorderStyle.THIN, cs.getBorderRight()); assertEquals("Right Border", BorderStyle.THIN, cs.getBorderRight());

View File

@ -58,7 +58,7 @@ public abstract class BaseTestFont {
@Test @Test
public final void testGetNumberOfFonts() throws IOException { public final void testGetNumberOfFonts() throws IOException {
Workbook wb = _testDataProvider.createWorkbook(); Workbook wb = _testDataProvider.createWorkbook();
int num0 = wb.getNumberOfFonts(); int num0 = wb.getNumberOfFontsAsInt();
Font f1=wb.createFont(); Font f1=wb.createFont();
f1.setBold(true); f1.setBold(true);