Browse Source

[bug-62018] use ints to index fonts

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1824826 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_4_0_0_FINAL
PJ Fanning 6 years ago
parent
commit
05b0de574c

+ 162
- 159
src/examples/src/org/apache/poi/hssf/view/SVTableCellEditor.java View File

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 white = getAWTColor(HSSFColorPredefined.WHITE);

private HSSFWorkbook wb;
private JTextField editor;

public SVTableCellEditor(HSSFWorkbook wb) {
this.wb = wb;
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;
private static final Color black = getAWTColor(HSSFColorPredefined.BLACK);
private static final Color white = getAWTColor(HSSFColorPredefined.WHITE);
private static final POILogger logger = POILogFactory.getLogger(SVTableCellEditor.class);

private HSSFWorkbook wb;
private JTextField editor;

public SVTableCellEditor(HSSFWorkbook wb) {
this.wb = wb;
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) {
return true;
}


public boolean startCellEditing(EventObject anEvent) {
logger.log(POILogger.INFO, "Start Cell Editing");
return true;
}


@Override
public boolean stopCellEditing() {
logger.log(POILogger.INFO, "Stop Cell Editing");
fireEditingStopped();
return true;
}


@Override
public void cancelCellEditing() {
logger.log(POILogger.INFO, "Cancel Cell Editing");
fireEditingCanceled();
}


@Override
public void actionPerformed(ActionEvent e) {
logger.log(POILogger.INFO, "Action performed");
stopCellEditing();
}


/**
* Gets the cellEditorValue attribute of the SVTableCellEditor object
*
* @return The cellEditorValue value
*/
@Override
public Object getCellEditorValue() {
logger.log(POILogger.INFO, "GetCellEditorValue");
//JMH Look at when this method is called. Should it return a HSSFCell?
return editor.getText();
} }
return false;
}


@Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}


public boolean startCellEditing(EventObject anEvent) {
System.out.println("Start Cell Editing");
return true;
}


@Override
public boolean stopCellEditing() {
System.out.println("Stop Cell Editing");
fireEditingStopped();
return true;
}


@Override
public void cancelCellEditing() {
System.out.println("Cancel Cell Editing");
fireEditingCanceled();
}


@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action performed");
stopCellEditing();
}


/**
* Gets the cellEditorValue attribute of the SVTableCellEditor object
*
* @return The cellEditorValue value
*/
@Override
public Object getCellEditorValue() {
System.out.println("GetCellEditorValue");
//JMH Look at when this method is called. Should it return a HSSFCell?
return editor.getText();
}


/**
* Gets the tableCellEditorComponent attribute of the SVTableCellEditor object
*
* @return The tableCellEditorComponent value
*/
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,
int row,
int column) {
System.out.println("GetTableCellEditorComponent");
HSSFCell cell = (HSSFCell) value;
if (cell != null) {
HSSFCellStyle style = cell.getCellStyle();
HSSFFont f = wb.getFontAt(style.getFontIndex());
boolean isbold = f.getBold();
boolean isitalics = f.getItalic();

int fontstyle = Font.PLAIN;

if (isbold) {
fontstyle = Font.BOLD;
}
if (isitalics) {
fontstyle = fontstyle | Font.ITALIC;
}

int fontheight = f.getFontHeightInPoints();
if (fontheight == 9) {
fontheight = 10; //fix for stupid ol Windows
}

Font font = new Font(f.getFontName(),fontstyle,fontheight);
editor.setFont(font);

if (style.getFillPattern() == FillPatternType.SOLID_FOREGROUND) {
editor.setBackground(getAWTColor(style.getFillForegroundColor(), white));
} else {
editor.setBackground(white);
}

editor.setForeground(getAWTColor(f.getColor(), black));


//Set the value that is rendered for the cell
switch (cell.getCellType()) {
case BLANK:
editor.setText("");
break;
case BOOLEAN:
if (cell.getBooleanCellValue()) {
editor.setText("true");
} else {
editor.setText("false");
}
break;
case NUMERIC:
editor.setText(Double.toString(cell.getNumericCellValue()));
break;
case STRING:
editor.setText(cell.getRichStringCellValue().getString());
break;
case FORMULA:
default:
editor.setText("?");
}
switch (style.getAlignment()) {
case LEFT:
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;
}



/**
* Gets the tableCellEditorComponent attribute of the SVTableCellEditor object
*
* @return The tableCellEditorComponent value
*/
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,
int row,
int column) {
logger.log(POILogger.INFO, "GetTableCellEditorComponent");
HSSFCell cell = (HSSFCell) value;
if (cell != null) {
HSSFCellStyle style = cell.getCellStyle();
HSSFFont f = wb.getFontAt(style.getFontIntIndex());
boolean isbold = f.getBold();
boolean isitalics = f.getItalic();

int fontstyle = Font.PLAIN;

if (isbold) {
fontstyle = Font.BOLD;
}
if (isitalics) {
fontstyle = fontstyle | Font.ITALIC;
}

int fontheight = f.getFontHeightInPoints();
if (fontheight == 9) {
fontheight = 10; //fix for stupid ol Windows
}

Font font = new Font(f.getFontName(), fontstyle, fontheight);
editor.setFont(font);

if (style.getFillPattern() == FillPatternType.SOLID_FOREGROUND) {
editor.setBackground(getAWTColor(style.getFillForegroundColor(), white));
} else {
editor.setBackground(white);
}

editor.setForeground(getAWTColor(f.getColor(), black));


//Set the value that is rendered for the cell
switch (cell.getCellType()) {
case BLANK:
editor.setText("");
break;
case BOOLEAN:
if (cell.getBooleanCellValue()) {
editor.setText("true");
} else {
editor.setText("false");
}
break;
case NUMERIC:
editor.setText(Double.toString(cell.getNumericCellValue()));
break;
case STRING:
editor.setText(cell.getRichStringCellValue().getString());
break;
case FORMULA:
default:
editor.setText("?");
}
switch (style.getAlignment()) {
case LEFT:
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;
}
} }

+ 2
- 2
src/examples/src/org/apache/poi/hssf/view/SVTableCellRenderer.java View File

@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


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) {

+ 1
- 1
src/examples/src/org/apache/poi/ss/examples/CellStyleDetails.java View File

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) {

+ 1
- 1
src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java View File

} }


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");

+ 1
- 1
src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java View File

@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)

+ 16
- 4
src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java View File

* 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) {
* @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.HSSFWorkbook#getFontAt(short)
* @see org.apache.poi.hssf.usermodel.HSSFCellStyle#getFontIntIndex()
* @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());
} }


/** /**

+ 13
- 5
src/java/org/apache/poi/hssf/usermodel/HSSFFont.java View File

* 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 {
public final static String FONT_ARIAL = "Arial"; public final static String FONT_ARIAL = "Arial";




private FontRecord font;
private short index;
private FontRecord font;
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;
* 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;
} }

+ 20
- 19
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java View File

* 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
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");
} }


String name, boolean italic, boolean strikeout, String name, boolean italic, boolean strikeout,
short typeOffset, byte underline) short typeOffset, byte underline)
{ {
short numberOfFonts = getNumberOfFonts();
for (short i=0; i<=numberOfFonts; i++) {
int numberOfFonts = getNumberOfFontsAsInt();
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;
return null; return null;
} }


/**
* get the number of fonts in the font table
* @return number of fonts
*/
@Override
@Deprecated
public short getNumberOfFonts() {
return (short)getNumberOfFontsAsInt();
}


@Override @Override
public short getNumberOfFonts()
{
return (short) workbook.getNumberOfFontRecords();
public int getNumberOfFontsAsInt() {
return workbook.getNumberOfFontRecords();
} }


/**
* Get the font at the given index number
* @param idx index number
* @return HSSFFont at the index
*/
@Override @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<>();
} }
// 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);
} }

+ 10
- 1
src/java/org/apache/poi/ss/usermodel/CellStyle.java View File

* 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

+ 13
- 0
src/java/org/apache/poi/ss/usermodel/Font.java View File

package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;




import org.apache.poi.util.Removal;

public interface Font { 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();

+ 21
- 0
src/java/org/apache/poi/ss/usermodel/Workbook.java View File

* 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
* *

+ 20
- 4
src/java/org/apache/poi/ss/util/CellUtil.java View File

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");
} }
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));
*/ */
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) {
return ((Short) value).shortValue();
if (value instanceof Number) {
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;
} }

+ 2
- 2
src/java/org/apache/poi/ss/util/SheetUtil.java View File

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) {
*/ */
@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);

+ 15
- 14
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java View File

{ {
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 (short)getNumberOfFontsAsInt();
}

@Override
public int getNumberOfFontsAsInt()
{ {
return _wb.getNumberOfFonts();
return _wb.getNumberOfFontsAsInt();
} }


/**
* Get the font at the given index number
*
* @param idx index number (0-based)
* @return font at the index
*/
@Override @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);
} }

+ 13
- 0
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java View File

* @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
* *

+ 9
- 7
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java View File

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
setFamily(family.getValue()); setFamily(family.getValue());
} }


/**
* 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)
*/
public short getIndex()
@Override
@Deprecated
public short getIndex() {
return (short)getIndexAsInt();
}

@Override
public int getIndexAsInt()
{ {
return _index; return _index;
} }

+ 12
- 11
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java View File



/** /**
* 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) {
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.
* *
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();
} }



+ 5
- 5
src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java View File

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();
} }

+ 1
- 1
src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java View File

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());

+ 1
- 1
src/testcases/org/apache/poi/ss/usermodel/BaseTestFont.java View File

@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);

Loading…
Cancel
Save