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
This commit is contained in:
Nick Burch 2011-03-04 15:05:18 +00:00
parent a9133e1b3b
commit 93e7799a7b
7 changed files with 53 additions and 13 deletions

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.8-beta2" date="2011-??-??"> <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">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">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> <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>

View File

@ -98,6 +98,9 @@ public class StylesTable extends POIXMLDocumentPart {
public void setTheme(ThemesTable theme) { public void setTheme(ThemesTable theme) {
this.theme = theme; this.theme = theme;
for(XSSFFont font : fonts) {
font.setThemesTable(theme);
}
} }
/** /**
@ -125,6 +128,7 @@ public class StylesTable extends POIXMLDocumentPart {
if(ctfonts != null){ if(ctfonts != null){
int idx = 0; int idx = 0;
for (CTFont font : ctfonts.getFontArray()) { for (CTFont font : ctfonts.getFontArray()) {
// Create the font and save it. Themes Table supplied later
XSSFFont f = new XSSFFont(font, idx); XSSFFont f = new XSSFFont(font, idx);
fonts.add(f); fonts.add(f);
idx++; idx++;

View 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.FontUnderline;
import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.model.StylesTable; 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.CTBooleanProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; 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(); public static final short DEFAULT_FONT_COLOR = IndexedColors.BLACK.getIndex();
private ThemesTable _themes;
private CTFont _ctFont; private CTFont _ctFont;
private short _index; private short _index;
@ -147,7 +149,15 @@ public class XSSFFont implements Font {
*/ */
public XSSFColor getXSSFColor() { public XSSFColor getXSSFColor() {
CTColor ctColor = _ctFont.sizeOfColorArray() == 0 ? null : _ctFont.getColorArray(0); 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 * to the style table
*/ */
public long registerTo(StylesTable styles) { public long registerTo(StylesTable styles) {
this._themes = styles.getTheme();
short idx = (short)styles.putFont(this, true); short idx = (short)styles.putFont(this, true);
this._index = idx; this._index = idx;
return 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. * 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 * @return unique index number of the underlying record this Font represents (probably you don't care
* unless you're comparing which one is which) * unless you're comparing which one is which)
*/ */
public short getIndex() public short getIndex()
{ {
return _index; return _index;

View File

@ -26,6 +26,7 @@ import javax.xml.namespace.QName;
import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.model.ThemesTable;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; 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++){ for(int i = 0; i < st.sizeOfRArray(); i++){
CTRElt r = st.getRArray(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; return null;
} }
@ -361,7 +366,11 @@ public class XSSFRichTextString implements RichTextString {
int pos = 0; int pos = 0;
for(int i = 0; i < st.sizeOfRArray(); i++){ for(int i = 0; i < st.sizeOfRArray(); i++){
CTRElt r = st.getRArray(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(); pos += r.getT().length();
} }
@ -543,4 +552,9 @@ public class XSSFRichTextString implements RichTextString {
} }
return st; return st;
} }
private ThemesTable getThemesTable() {
if(styles == null) return null;
return styles.getTheme();
}
} }

View File

@ -742,10 +742,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
* Fonts where their colours come from the theme rather * Fonts where their colours come from the theme rather
* then being set explicitly still should allow the * then being set explicitly still should allow the
* fetching of the RGB. * 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"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50784-font_theme_colours.xlsx");
XSSFSheet s = wb.getSheetAt(0); XSSFSheet s = wb.getSheetAt(0);
XSSFRow r = s.getRow(0); XSSFRow r = s.getRow(0);
@ -766,8 +764,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
assertEquals(9, colt.getTheme()); assertEquals(9, colt.getTheme());
XSSFColor themeC = wb.getTheme().getThemeColor(colt.getTheme()); XSSFColor themeC = wb.getTheme().getThemeColor(colt.getTheme());
assertNotNull( themeC.getRgb() ); assertNotNull( themeC.getRgb() );
// TODO Fix it so this works
assertNotNull( colt.getRgb() ); assertNotNull( colt.getRgb() );
assertEquals( themeC.getRgb(), colt.getRgb() ); // The same colour assertEquals( themeC.getARGBHex(), colt.getARGBHex() ); // The same colour
} }
} }

View File

@ -18,7 +18,13 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import org.apache.poi.POIXMLException; 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.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.XSSFTestDataSamples;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;

View File

@ -17,13 +17,13 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import java.util.TreeMap;
import junit.framework.TestCase; 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.CTRst;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring; 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 * Tests functionality of the XSSFRichTextRun object