Bug 58068: Add a method to pass the actual Color to StylesTable.findFont().

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1811866 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-10-11 18:32:01 +00:00
parent 2dc64ebbcc
commit acb78e74c4
2 changed files with 132 additions and 10 deletions

View File

@ -36,10 +36,7 @@ import java.util.TreeMap;
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;
@ -828,7 +825,8 @@ public class StylesTable extends POIXMLDocumentPart {
}
/**
* 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) {
@ -847,6 +845,27 @@ public class StylesTable extends POIXMLDocumentPart {
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
*/

View File

@ -17,11 +17,6 @@
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;
@ -51,6 +46,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;
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() {
@ -337,4 +334,110 @@ public final class TestXSSFFont extends BaseTestFont{
// 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();
}
}