]> source.dussan.org Git - poi.git/commitdiff
Bug 62272: Include alpha/transparency value when setting a color-value for a font
authorDominik Stadler <centic@apache.org>
Mon, 30 Jan 2023 12:19:24 +0000 (12:19 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 30 Jan 2023 12:19:24 +0000 (12:19 +0000)
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

poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFFont.java
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFFont.java
test-data/spreadsheet/62272.xlsx [new file with mode: 0644]

index 0f97f6e4aff1a815321ebd508322651279d63a4b..41e903907b780e05166f19cadb197707537bf9a9 100644 (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());
         }
     }
 
index 9553d595bbd4264d6d0434c0957ee7a6eb35da45..35bcd75f00b74ae9aab31741a5dab0d3c8f253a4 100644 (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());
+    }
 }
diff --git a/test-data/spreadsheet/62272.xlsx b/test-data/spreadsheet/62272.xlsx
new file mode 100644 (file)
index 0000000..9b91105
Binary files /dev/null and b/test-data/spreadsheet/62272.xlsx differ