From 93e7799a7bdf06cc5679d389308383f2264d097e Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 4 Mar 2011 15:05:18 +0000 Subject: [PATCH] Fix bug #50784 - XSSFColors return by XSSFFont now have theme information applied to them, as XSSFFont is now ThemesTable aware git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1077986 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/xssf/model/StylesTable.java | 4 ++++ .../apache/poi/xssf/usermodel/XSSFFont.java | 22 +++++++++++++++++-- .../xssf/usermodel/XSSFRichTextString.java | 18 +++++++++++++-- .../poi/xssf/usermodel/TestXSSFBugs.java | 7 ++---- .../poi/xssf/usermodel/TestXSSFFont.java | 8 ++++++- .../usermodel/TestXSSFRichTextString.java | 6 ++--- 7 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 1ef1f8335..8ddac5f12 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 50784 - XSSFColors return by XSSFFont now have theme information applied to them 50846 - Improve how XSSFColor inherits from Themes 50847 - XSSFFont now accepts the full range of Charsets from FontChartset 50786 - Speed up calls to HSSFColor.getIndexHash() by returning a cached, unmodifiable Map. HSSFColor.getModifiableIndexHash() provides access to the old (slow but modifiable) functionality diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index 31844afb8..60dad91b0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -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++; diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java index cdf93c8ff..bb9711ec6 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java @@ -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; diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index 1809b7c04..964e455e3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -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(); + } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 465d164c2..d71edf77b 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -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 } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java index b55833f41..5d93fa121 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java @@ -18,7 +18,13 @@ 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; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java index 0b925fc95..b53fe28fe 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java @@ -17,13 +17,13 @@ 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