Browse Source

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
tags/REL_5_2_4
Dominik Stadler 1 year ago
parent
commit
acdf2ec981

+ 5
- 12
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFFont.java View File

@@ -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());
}
}


+ 52
- 0
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFFont.java View File

@@ -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());
}
}

BIN
test-data/spreadsheet/62272.xlsx View File


Loading…
Cancel
Save