import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.ss.SpreadsheetVersion;
-import org.apache.poi.ss.usermodel.BuiltinFormats;
-import org.apache.poi.ss.usermodel.FontFamily;
-import org.apache.poi.ss.usermodel.FontScheme;
-import org.apache.poi.ss.usermodel.TableStyle;
+import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.Internal;
import org.apache.poi.xssf.usermodel.CustomIndexedColorMap;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
}
/**
- * Finds a font that matches the one with the supplied attributes
+ * Finds a font that matches the one with the supplied attributes,
+ * where color is the indexed-value, not the actual color.
*/
public XSSFFont findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
for (XSSFFont font : fonts) {
return null;
}
+ /**
+ * Finds a font that matches the one with the supplied attributes,
+ * where color is the actual Color-value, not the indexed color
+ */
+ public XSSFFont findFont(boolean bold, Color color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
+ for (XSSFFont font : fonts) {
+ if ( (font.getBold() == bold)
+ && font.getXSSFColor().equals(color)
+ && font.getFontHeight() == fontHeight
+ && font.getFontName().equals(name)
+ && font.getItalic() == italic
+ && font.getStrikeout() == strikeout
+ && font.getTypeOffset() == typeOffset
+ && font.getUnderline() == underline)
+ {
+ return font;
+ }
+ }
+ return null;
+ }
+
/**
* @return default or custom indexed color to RGB mapping
*/
package org.apache.poi.xssf.usermodel;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
-
import java.io.IOException;
import org.apache.poi.POIXMLException;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;
+import static org.junit.Assert.*;
+
public final class TestXSSFFont extends BaseTestFont{
public TestXSSFFont() {
// Even with invalid fonts we still get back useful data most of the time...
SheetUtil.canComputeColumnWidth(font);
}
+
+ /**
+ * Test that fonts get added properly
+ */
+ @Test
+ public void testFindFont() throws IOException {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ assertEquals(1, wb.getNumberOfFonts());
+
+ XSSFSheet s = wb.createSheet();
+ s.createRow(0);
+ s.createRow(1);
+ s.getRow(0).createCell(0);
+ s.getRow(1).createCell(0);
+
+ assertEquals(1, wb.getNumberOfFonts());
+
+ XSSFFont f1 = wb.getFontAt((short) 0);
+ assertFalse(f1.getBold());
+
+ // Check that asking for the same font
+ // multiple times gives you the same thing.
+ // Otherwise, our tests wouldn't work!
+ assertSame(wb.getFontAt((short) 0), wb.getFontAt((short) 0));
+ assertEquals(
+ wb.getFontAt((short) 0),
+ wb.getFontAt((short) 0)
+ );
+
+ // Look for a new font we have
+ // yet to add
+ assertNull(
+ wb.findFont(
+ false, IndexedColors.INDIGO.getIndex(), (short) 22,
+ "Thingy", false, true, (short) 2, (byte) 2
+ )
+ );
+ assertNull(
+ wb.getStylesSource().findFont(
+ false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22,
+ "Thingy", false, true, (short) 2, (byte) 2
+ )
+ );
+
+ XSSFFont nf = wb.createFont();
+ assertEquals(2, wb.getNumberOfFonts());
+
+ assertEquals(1, nf.getIndex());
+ assertEquals(nf, wb.getFontAt((short) 1));
+
+ nf.setBold(false);
+ nf.setColor(IndexedColors.INDIGO.getIndex());
+ nf.setFontHeight((short) 22);
+ nf.setFontName("Thingy");
+ nf.setItalic(false);
+ nf.setStrikeout(true);
+ nf.setTypeOffset((short) 2);
+ nf.setUnderline((byte) 2);
+
+ assertEquals(2, wb.getNumberOfFonts());
+ assertEquals(nf, wb.getFontAt((short) 1));
+
+ assertTrue(
+ wb.getFontAt((short) 0)
+ !=
+ wb.getFontAt((short) 1)
+ );
+
+ // Find it now
+ assertNotNull(
+ wb.findFont(
+ false, IndexedColors.INDIGO.getIndex(), (short) 22,
+ "Thingy", false, true, (short) 2, (byte) 2
+ )
+ );
+ assertNotNull(
+ wb.getStylesSource().findFont(
+ false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22,
+ "Thingy", false, true, (short) 2, (byte) 2
+ )
+ );
+
+ XSSFFont font = wb.findFont(
+ false, IndexedColors.INDIGO.getIndex(), (short) 22,
+ "Thingy", false, true, (short) 2, (byte) 2
+ );
+ assertNotNull(font);
+ assertEquals(
+ 1,
+ font.getIndex()
+ );
+ assertEquals(nf,
+ wb.findFont(
+ false, IndexedColors.INDIGO.getIndex(), (short) 22,
+ "Thingy", false, true, (short) 2, (byte) 2
+ )
+ );
+ assertEquals(nf,
+ wb.getStylesSource().findFont(
+ false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22,
+ "Thingy", false, true, (short) 2, (byte) 2
+ )
+ );
+
+ wb.close();
+ }
}