]> source.dussan.org Git - poi.git/commitdiff
Fix bug #50784 - XSSFColors return by XSSFFont now have theme information applied...
authorNick Burch <nick@apache.org>
Fri, 4 Mar 2011 15:05:18 +0000 (15:05 +0000)
committerNick Burch <nick@apache.org>
Fri, 4 Mar 2011 15:05:18 +0000 (15:05 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1077986 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java

index 1ef1f8335ee4846eaebafba9a66bb575d2ba2aff..8ddac5f12e0f3b98a5c97719e35c4a294b24f06c 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta2" date="2011-??-??">
+           <action dev="poi-developers" type="fix">50784 - XSSFColors return by XSSFFont now have theme information applied to them</action>
            <action dev="poi-developers" type="fix">50846 - Improve how XSSFColor inherits from Themes</action>
            <action dev="poi-developers" type="fix">50847 - XSSFFont now accepts the full range of Charsets from FontChartset</action>
            <action dev="poi-developers" type="fix">50786 - Speed up calls to HSSFColor.getIndexHash() by returning a cached, unmodifiable Map. HSSFColor.getModifiableIndexHash() provides access to the old (slow but modifiable) functionality</action>
index 31844afb8e97c2c1a6f63a514f551010872313fd..60dad91b0b151f80ac952f1292a1f13c7d7bbbc9 100644 (file)
@@ -98,6 +98,9 @@ public class StylesTable extends POIXMLDocumentPart {
 
     public void setTheme(ThemesTable theme) {
         this.theme = theme;
+        for(XSSFFont font : fonts) {
+           font.setThemesTable(theme);
+        }
     }
 
        /**
@@ -125,6 +128,7 @@ public class StylesTable extends POIXMLDocumentPart {
             if(ctfonts != null){
                                int idx = 0;
                                for (CTFont font : ctfonts.getFontArray()) {
+                                  // Create the font and save it. Themes Table supplied later
                                        XSSFFont f = new XSSFFont(font, idx);
                                        fonts.add(f);
                                        idx++;
index cdf93c8ff37310ce97ec600d3186a5962dbcd288..bb9711ec644ed008b8a4cb2223aa667b3804bcea 100644 (file)
@@ -25,6 +25,7 @@ import org.apache.poi.ss.usermodel.FontScheme;
 import org.apache.poi.ss.usermodel.FontUnderline;
 import org.apache.poi.ss.usermodel.IndexedColors;
 import org.apache.poi.xssf.model.StylesTable;
+import org.apache.poi.xssf.model.ThemesTable;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
@@ -59,6 +60,7 @@ public class XSSFFont implements Font {
      */
     public static final short DEFAULT_FONT_COLOR = IndexedColors.BLACK.getIndex();
 
+    private ThemesTable _themes;
     private CTFont _ctFont;
     private short _index;
 
@@ -147,7 +149,15 @@ public class XSSFFont implements Font {
      */
     public XSSFColor getXSSFColor() {
         CTColor ctColor = _ctFont.sizeOfColorArray() == 0 ? null : _ctFont.getColorArray(0);
-        return ctColor == null ? null : new XSSFColor(ctColor);
+        if(ctColor != null) {
+           XSSFColor color = new XSSFColor(ctColor);
+           if(_themes != null) {
+              _themes.inheritFromThemeAsRequired(color);
+           }
+           return color;
+        } else {
+           return null;
+        }
     }
 
 
@@ -520,10 +530,19 @@ public class XSSFFont implements Font {
      *  to the style table
      */
     public long registerTo(StylesTable styles) {
+        this._themes = styles.getTheme();
         short idx = (short)styles.putFont(this, true);
         this._index = idx;
         return idx;
     }
+    /**
+     * Records the Themes Table that is associated with
+     *  the current font, used when looking up theme
+     *  based colours and properties.
+     */
+    public void setThemesTable(ThemesTable themes) {
+       this._themes = themes;
+    }
 
     /**
      * get the font scheme property.
@@ -589,7 +608,6 @@ public class XSSFFont implements Font {
      * @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()
     {
         return _index;
index 1809b7c0404dcac9b6b817c50011fef0a6ee723f..964e455e311997fc4ef5abc1940e12425b1e4cec 100644 (file)
@@ -26,6 +26,7 @@ import javax.xml.namespace.QName;
 import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.xssf.model.StylesTable;
+import org.apache.poi.xssf.model.ThemesTable;
 import org.apache.poi.util.Internal;
 import org.apache.xmlbeans.XmlCursor;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
@@ -342,7 +343,11 @@ public class XSSFRichTextString implements RichTextString {
 
         for(int i = 0; i < st.sizeOfRArray(); i++){
             CTRElt r = st.getRArray(i);
-            if(i == index) return new XSSFFont(toCTFont(r.getRPr()));
+            if(i == index) {
+               XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr()));
+               fnt.setThemesTable(getThemesTable());
+               return fnt;
+            }
         }
         return null;
     }
@@ -361,7 +366,11 @@ public class XSSFRichTextString implements RichTextString {
         int pos = 0;
         for(int i = 0; i < st.sizeOfRArray(); i++){
             CTRElt r = st.getRArray(i);
-            if(index >= pos && index < pos + r.getT().length()) return new XSSFFont(toCTFont(r.getRPr()));
+            if(index >= pos && index < pos + r.getT().length()) {
+               XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr()));
+               fnt.setThemesTable(getThemesTable());
+               return fnt;
+            }
 
             pos += r.getT().length();
         }
@@ -543,4 +552,9 @@ public class XSSFRichTextString implements RichTextString {
         }
         return st;
     }
+    
+    private ThemesTable getThemesTable() {
+       if(styles == null) return null;
+       return styles.getTheme();
+    }
 }
index 465d164c269a1ae4602d30f080c09825519ebb8a..d71edf77b4831b69a7c51b1de4798ee07dccb120 100644 (file)
@@ -742,10 +742,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
      * Fonts where their colours come from the theme rather
      *  then being set explicitly still should allow the
      *  fetching of the RGB.
-     * TODO Allow XSSFFont to get at the themes table, so it can do
-     *  the same trick that XSSFCellStyle does with theme colours 
      */
-    public void DISABLEDtest50784() throws Exception {
+    public void test50784() throws Exception {
        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50784-font_theme_colours.xlsx");
        XSSFSheet s = wb.getSheetAt(0);
        XSSFRow r = s.getRow(0);
@@ -766,8 +764,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
        assertEquals(9, colt.getTheme());
        XSSFColor themeC = wb.getTheme().getThemeColor(colt.getTheme());
        assertNotNull( themeC.getRgb() );
-       // TODO Fix it so this works
        assertNotNull( colt.getRgb() );
-       assertEquals( themeC.getRgb(), colt.getRgb() ); // The same colour
+       assertEquals( themeC.getARGBHex(), colt.getARGBHex() ); // The same colour
     }
 }
index b55833f4199aaa9ed8645a874e4b57389dff313d..5d93fa121510de393684eddb84a7e0003341e055 100644 (file)
 package org.apache.poi.xssf.usermodel;
 
 import org.apache.poi.POIXMLException;
-import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.usermodel.BaseTestFont;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.FontCharset;
+import org.apache.poi.ss.usermodel.FontFamily;
+import org.apache.poi.ss.usermodel.FontScheme;
+import org.apache.poi.ss.usermodel.FontUnderline;
+import org.apache.poi.ss.usermodel.IndexedColors;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
index 0b925fc9549db0e10014cfcff0f8041d38872db8..b53fe28fedaf804721ace81a6eddc4e2be1fa999 100644 (file)
 
 package org.apache.poi.xssf.usermodel;
 
+import java.util.TreeMap;
+
 import junit.framework.TestCase;
 
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
-
-import java.util.TreeMap;
 
 /**
  * Tests functionality of the XSSFRichTextRun object