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
This commit is contained in:
Dominik Stadler 2023-01-30 12:19:24 +00:00
parent a7382f00b9
commit acdf2ec981
3 changed files with 57 additions and 12 deletions

View File

@ -392,17 +392,10 @@ public class XSSFFont implements Font {
@Override @Override
public void setColor(short color) { public void setColor(short color) {
CTColor ctColor = _ctFont.sizeOfColorArray() == 0 ? _ctFont.addNewColor() : _ctFont.getColorArray(0); CTColor ctColor = _ctFont.sizeOfColorArray() == 0 ? _ctFont.addNewColor() : _ctFont.getColorArray(0);
switch (color) { if (color == Font.COLOR_NORMAL) {
case Font.COLOR_NORMAL: { ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);
ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR); } else {
break; ctColor.setIndexed(color);
}
case Font.COLOR_RED: {
ctColor.setIndexed(IndexedColors.RED.getIndex());
break;
}
default:
ctColor.setIndexed(color);
} }
} }
@ -418,7 +411,7 @@ public class XSSFFont implements Font {
if (ctColor.isSetIndexed()) { if (ctColor.isSetIndexed()) {
ctColor.unsetIndexed(); ctColor.unsetIndexed();
} }
ctColor.setRgb(color.getRGB()); ctColor.setRgb(color.getARGB());
} }
} }

View File

@ -21,16 +21,20 @@ import static org.apache.poi.ss.usermodel.FontCharset.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.apache.poi.common.usermodel.fonts.FontCharset; import org.apache.poi.common.usermodel.fonts.FontCharset;
import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.ss.usermodel.BaseTestFont; 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.Font;
import org.apache.poi.ss.usermodel.FontFamily; import org.apache.poi.ss.usermodel.FontFamily;
import org.apache.poi.ss.usermodel.FontScheme; 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.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.SheetUtil; import org.apache.poi.ss.util.SheetUtil;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
@ -495,4 +499,52 @@ public final class TestXSSFFont extends BaseTestFont {
notequ.setThemeColor((short)123); notequ.setThemeColor((short)123);
assertNotEquals(font, notequ); 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());
}
} }

Binary file not shown.