You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2017/10/11 18:32:01 UTC

svn commit: r1811866 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/model/StylesTable.java testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java

Author: centic
Date: Wed Oct 11 18:32:01 2017
New Revision: 1811866

URL: http://svn.apache.org/viewvc?rev=1811866&view=rev
Log:
Bug 58068: Add a method to pass the actual Color to StylesTable.findFont().

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java?rev=1811866&r1=1811865&r2=1811866&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java Wed Oct 11 18:32:01 2017
@@ -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 POIXMLD
     }
     
     /**
-     * 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) {
@@ -848,6 +846,27 @@ public class StylesTable extends POIXMLD
     }
     
     /**
+     * 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
      */
     public IndexedColorMap getIndexedColors() {

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java?rev=1811866&r1=1811865&r2=1811866&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java Wed Oct 11 18:32:01 2017
@@ -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.spread
 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
         // 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();
+	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org