diff options
author | Dominik Stadler <centic@apache.org> | 2023-01-30 12:19:24 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2023-01-30 12:19:24 +0000 |
commit | acdf2ec9815fd01d9a9b9db3752fc70dd3914059 (patch) | |
tree | b42e300353a7af8103ed62bb58ae5c0906d86cf2 /poi-ooxml | |
parent | a7382f00b948dd627df529aeadc8646ed95f40d3 (diff) | |
download | poi-acdf2ec9815fd01d9a9b9db3752fc70dd3914059.tar.gz poi-acdf2ec9815fd01d9a9b9db3752fc70dd3914059.zip |
Bug 62272: Include alpha/transparency value when setting a color-value for a font
Use method with returns 4 bytes to use an ARGB value instead of only RGB
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1907105 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFFont.java | 17 | ||||
-rw-r--r-- | poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFFont.java | 52 |
2 files changed, 57 insertions, 12 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFFont.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFFont.java index 0f97f6e4af..41e903907b 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFFont.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFFont.java @@ -392,17 +392,10 @@ public class XSSFFont implements Font { @Override public void setColor(short color) { CTColor ctColor = _ctFont.sizeOfColorArray() == 0 ? _ctFont.addNewColor() : _ctFont.getColorArray(0); - switch (color) { - case Font.COLOR_NORMAL: { - ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR); - break; - } - case Font.COLOR_RED: { - ctColor.setIndexed(IndexedColors.RED.getIndex()); - break; - } - default: - ctColor.setIndexed(color); + if (color == Font.COLOR_NORMAL) { + ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR); + } else { + ctColor.setIndexed(color); } } @@ -418,7 +411,7 @@ public class XSSFFont implements Font { if (ctColor.isSetIndexed()) { ctColor.unsetIndexed(); } - ctColor.setRgb(color.getRGB()); + ctColor.setRgb(color.getARGB()); } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFFont.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFFont.java index 9553d595bb..35bcd75f00 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFFont.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFFont.java @@ -21,16 +21,20 @@ import static org.apache.poi.ss.usermodel.FontCharset.*; import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; +import java.util.Arrays; import java.util.stream.Stream; import org.apache.poi.common.usermodel.fonts.FontCharset; import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.ss.usermodel.BaseTestFont; +import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Font; 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.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.SheetUtil; import org.apache.poi.util.LocaleUtil; @@ -495,4 +499,52 @@ public final class TestXSSFFont extends BaseTestFont { notequ.setThemeColor((short)123); assertNotEquals(font, notequ); } + + @Test + public void testBug62272() throws IOException { + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("62272.xlsx")) { + // make sure we read the font-color with alpha + checkFontColor(wb); + } + } + + @Test + public void testBug62272a() throws IOException { + try (XSSFWorkbook wb = new XSSFWorkbook()) { + Sheet sheet = wb.createSheet("test"); + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); + + // create a font with alpha + XSSFFont font = wb.createFont(); + font.setColor(new XSSFColor(new byte[] { + 0x7f, 0x33, (byte)0xCC, 0x66 + })); + + XSSFCellStyle style = wb.createCellStyle(); + style.setFont(font); + + cell.setCellStyle(style); + cell.setCellValue("testtext"); + + // make sure the alpha-value was stored properly + checkFontColor(wb); + + /*try (OutputStream out = new FileOutputStream("/tmp/testout.xlsx")) { + wb.write(out); + }*/ + } + } + + private static void checkFontColor(Workbook wb) { + int fontIdx = wb.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFontIndex(); + Font font = wb.getFontAt(fontIdx); + //System.out.println(font.getColor()); + + CTColor[] colorArray = ((XSSFFont) font).getCTFont().getColorArray(); + //System.out.println(Arrays.toString(colorArray)); + assertArrayEquals(new byte[] { + 0x7f, 0x33, (byte)0xCC, 0x66 + }, colorArray[0].getRgb()); + } } |